<!-- eslint-disable no-param-reassign -->
<!-- eslint-disable no-console -->
<script setup lang="ts">
import { onBeforeUnmount } from 'vue'
import { onLaunch, onShow, onHide, onUnload } from '@dcloudio/uni-app'
import { storeToRefs } from 'pinia'
import { useUserStore } from '@/store'

const userStore = useUserStore()
const { accessToken } = storeToRefs(userStore)

onLaunch(() => {
  console.log('App Launch')
  if (uni.getSystemInfoSync().platform === 'android') {
    // Android 使用原生权限申请
    plus.android.requestPermissions(
      [
        'android.permission.READ_EXTERNAL_STORAGE',
        'android.permission.WRITE_EXTERNAL_STORAGE',
        'android.permission.CAMERA'
      ],
      function (res) {
        console.log('Android权限申请结果:', res)
      },
      function (err) {
        console.error('Android权限申请失败:', err)
      }
    )
  } else if (uni.getSystemInfoSync().platform === 'ios') {
    // iOS 权限已通过 manifest.json 的隐私描述声明，通常无需动态申请
  }
  uni.onNetworkStatusChange((res) => {
    const { isConnected, networkType } = res
    if (['2g', '3g', '4g', '5g'].includes(networkType)) {
      uni.showToast({
        icon: 'none',
        title: '当前网络为移动数据流量，请注意流量使用情况'
      })
    }
  })
})
onShow(async () => {
  console.log('App Show')
  if (!accessToken.value) {
    userStore.$patch((v) => {
      v.accessToken = uni.getStorageSync('accessToken') || ''
      v.userInfo = uni.getStorageSync('userInfo') || null
    })
  }
})
onHide(() => {
  console.log('App Hide')
})
onBeforeUnmount(() => {
  console.log('App Before Unmount')
})
</script>
<style lang="scss">
@import 'vk-uview-ui/index.scss';
</style>
